package com.zucai365.football.service.impl;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.wcj.common.service.impl.WcjServiceImpl;
import com.zucai365.football.bean.FootballTeamBean;
import com.zucai365.football.bean.LianSaiBean;
import com.zucai365.football.bean.LianSaiInfoBean;
import com.zucai365.football.bean.LianSaiYearBean;
import com.zucai365.football.dao.FootballTeamDao;
import com.zucai365.football.service.CurrentFootballTeamService;
import com.zucai365.football.service.FootballTeamService;
import com.zucai365.football.utils.ZuCaiUtils;

@Service("footballTeamService")
public class FootballTeamServiceImpl extends WcjServiceImpl<FootballTeamBean> implements FootballTeamService{
	
	@Autowired
	@Qualifier("footballTeamDao")
	private FootballTeamDao footballTeamDao;

	public FootballTeamDao getFootballTeamDao() {
		return footballTeamDao;
	}

	public void setFootballTeamDao( FootballTeamDao footballTeamDao ) {
		this.footballTeamDao = footballTeamDao;
	}
	
	@Autowired
	@Qualifier("currentFootballTeamService")
	private CurrentFootballTeamService currentFootballTeamService;
	
	public CurrentFootballTeamService getCurrentFootballTeamService() {
		return currentFootballTeamService;
	}

	public void setCurrentFootballTeamService(
			CurrentFootballTeamService currentFootballTeamService) {
		this.currentFootballTeamService = currentFootballTeamService;
	}

	@Override
	public void saveTeam( LianSaiInfoBean lianSaiInfo , String teamJson ) {
		
		LianSaiBean lianSai = lianSaiInfo.getLianSai();
		LianSaiYearBean lianSaiYear = lianSaiInfo.getLianSaiYear();
		
		if( StringUtils.isEmpty( teamJson ) ){		
			return;
		}
		
		List<FootballTeamBean> footballTeamList = new ArrayList<FootballTeamBean>();
		
		JSONArray jsonArray = JSONArray.fromObject( teamJson );
		for( int i=0;i<jsonArray.size();i++ ){
			JSONArray teamArray = (JSONArray)jsonArray.get(i);
			Integer teamId = (Integer)teamArray.get(0);
			String teamName = (String)teamArray.get(1);
			String teamImagePath = (String)teamArray.get(5);
			//该球队的所有名称
			String sumTeamName = teamName + "," +(String)teamArray.get(2) + "," + (String)teamArray.get(3) + "," + (String)teamArray.get(4);
			
			FootballTeamBean footballTeam = this.getFootballTeamByBet007Id( Long.valueOf( teamId ) );
			if( footballTeam == null ){
				footballTeam = new FootballTeamBean();
			}
			
			footballTeam.setFootballTeamName( teamName );
			footballTeam.setCountry( lianSai.getCountry() );
			footballTeam.setSumTeamName( sumTeamName );
			footballTeam.setBet007Id( Long.valueOf( teamId ) );
			footballTeam.setTeamImagePath( teamImagePath );
			footballTeamList.add( footballTeam );
		}
		
		this.footballTeamDao.saveAll( footballTeamList );
		this.currentFootballTeamService.saveCurrentYearFootBallInfo(lianSai, lianSaiYear, footballTeamList);
		
	}

	@Override
	public FootballTeamBean getFootballTeamByBet007Id(Long bet007Id) {
		// TODO Auto-generated method stub
		return this.footballTeamDao.findUniqueObj( "bet007Id", bet007Id );
	}
	
}
